<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<c:import url="/includes/header.jsp"></c:import>
</head>
<body id="containerBody">
	<div class="container">
		<div class="row">
			<c:forEach items="${list}" var="s">
				<div class="col-sm-6 thumbnail" style="margin-bottom: 0px">
					<div class="col-sm-4">
						<img style="width: 121px; height: 121px"
							src='${pageContext.request.contextPath}/images/admin/${s.logo}'
							alt="...">
					</div>
					<div class="col-sm-8">
						<c:if test="${s.state==0 }">
							<%-- 							<c:if test="${s.shop_type=='mt' }"> --%>
							<!-- 								<div class="row"> -->
							<!-- 									<input class="form-control" type="text" id="shopId" -->
							<!-- 										name="shopId" placeholder="商铺ID"> -->
							<!-- 								</div> -->
							<%-- 							</c:if> --%>
							<div class="row" style="float: right">
								<a class="btn btn-primary btn-sm bound"
									data-type="${s.shop_type}">绑定商铺</a>
							</div>
						</c:if>
						<c:if test="${s.state==1 }">

							<div class="row">
								<form class="form-horizontal">
									<!-- 									<div class="form-group"> -->
									<!-- 										<label for="inputEmail3" class="col-sm-3">LOGO:</label> -->
									<!-- 										<div class="col-sm-9"> -->
									<%-- 											<img src="${s.shop.imageUrl}"> --%>
									<!-- 										</div> -->
									<!-- 									</div> -->
									<div class="form-group">
										<label for="inputEmail3" class="col-sm-3">店铺名:</label>
										<div class="col-sm-9">${s.shop.name}</div>
									</div>
									<div class="form-group">
										<label for="inputPassword3" class="col-sm-3">店铺ID:</label>
										<div class="col-sm-9">${s.shop.ePoiId}</div>
									</div>
									<div class="form-group">
										<label for="inputPassword3" class="col-sm-3">店铺地址:</label>
										<div class="col-sm-9">${s.shop.address}</div>
									</div>
									<div class="form-group">
										<label for="inputPassword3" class="col-sm-3">联系电话:</label>
										<div class="col-sm-9">${s.shop.phone}</div>
									</div>
									<div class="form-group">
										<label for="inputPassword3" class="col-sm-3">营业状态:</label>
										<div class="col-sm-9">
											<p>
												<select class="btn btn-info shop_open" data-id="${s.shopId}"
													data-type="${s.shop_type}" style="padding: 0px">
													<option value="0"
														${s.shop.isOpen!=1?'selected="selected"':''}>休息中</option>
													<option value="1"
														${s.shop.isOpen==1?'selected="selected"':''}>营业中</option>
												</select>
											</p>

										</div>
									</div>
									<div class="form-group">
										<label for="inputPassword3" class="col-sm-3">自动接单:</label>
										<div class="col-sm-9">
											<div class="switch" data-on="success" data-off="warning"
												data-id="${s.id }">
												<input type="radio" name="orderAuto" value="2"
													${s.orderAuto==2?'checked':''} />云接单 <input type="radio"
													name="orderAuto" value="1" ${s.orderAuto==1?'checked':''} />本地接单
												<input type="radio" name="orderAuto" value="0"
													${s.orderAuto==0?'checked':''} />关闭
											</div>
										</div>
									</div>

									<div class="form-group">
										<label for="inputPassword3" class="col-sm-3">配送设置:</label>
										<div class="col-sm-9 switch_deliver" data-id="${s.id }">
											<p>
												<input type="radio" name="deliverType" value="0"
													${s.deliverType==0?'checked':''} />平台配送
											</p>
											<p>
												<input type="radio" name="deliverType" value="1"
													${s.deliverType==1?'checked':''} />达达配送 <span> <input
													type="text" name="deliverUid" placeholder="输入商户ID"
													class="deliver_dada" value="${s.deliverUid}"
													style="width: 80px"> <input type="text"
													name="deliverShopNo" placeholder="输入门店编号"
													class="deliver_dada" value="${s.deliverShopNo}"
													style="width: 80px">
												</span>
											</p>
											<!-- 											<p> -->
											<!-- 												<input type="radio" name="deliverAuto" value="0" -->
											<%-- 													${s.deliverAuto==0?'checked':''} />手工发单 <input --%>
											<!-- 													type="radio" name="deliverAuto" value="1" -->
											<%-- 													${s.deliverAuto==1?'checked':''} />自动发单 --%>
											<!-- 											</p> -->
										</div>
									</div>
								</form>
							</div>
						</c:if>
					</div>
				</div>
			</c:forEach>
		</div>
	</div>
</body>
</html>
<script type="text/javascript">
	$(function() {
		$(".bound")
				.click(
						function() {
							var shop_type = $(this).attr("data-type");
							var shopId = $("#shopId").val();
							var data = {
								shopId : shopId,
								shop_type : shop_type
							}
							var url = '${pageContext.request.contextPath}/admin/shop/auth.do?shop_type='
									+ shop_type + '&shopId=' + shopId;

							$
									.ajax({
										type : "post",
										dataType : "json",
										url : url,
										async : false,
										success : function(data) {
											if (data.code == 0) {
												if (shop_type == "ele") {
													top.window.location.href = data.data;
												} else {
													window.location.href = data.data;
												}
											}
										},
										error : function() {
											bootbox.alert("网络不稳定,运行失败");
										}
									});
						});
		//自动接单
		$(".switch")
				.find("input[type=radio]")
				.click(
						function() {
							var o = this;
							var id = $(o).parent().attr("data-id");
							var value = $(o).val();
							if (value == 1) {
								var isUse = top.nativeVaildPrinter();
								if (!isUse) {
									bootbox.alert("未查找到任何打印服务,请先设置默认打印机",
											function() {
												window.location.reload();
											});
									return;
								}
							}
							var url = "${pageContext.request.contextPath}/admin/shop/order/switch.do";
							$.ajax({
								type : "post",
								dataType : "json",
								data : {
									"orderAuto" : value,
									"id" : id
								},
								url : url,
								async : false,
								success : function(data) {
									if (data.code == 0) {
										$(o).attr("checked", true);
									} else {
										bootbox.alert(data.msg);
										window.location.reload();
									}
								},
								error : function() {
									bootbox.alert("网络不稳定,运行失败");
								}
							});
						});

		//配送设置
		$(".switch_deliver").find("input[type=radio]").click(function() {
			var o = this;
			var id = $(o).parent().parent().attr("data-id");
			var value = $(o).val();
			console.info(value);
			var deliverUid = $(this).parent().parent().find(".deliver").val();
			var data = {
				"deliverType" : value,
				"id" : id,
				"deliverUid" : deliverUid
			}
			httpDeliverSubmit(data);
		});

		$(".deliver_dada")
				.change(
						function() {
							var id = $(this).parent().parent().parent().attr(
									"data-id");
							var value = $(this).parent().prev().val();
							var deliverUid = $(this).parent().find(
									"input[name=deliverUid]").val();
							var deliverShopNo = $(this).parent().find(
									"input[name=deliverShopNo]").val();
							var data = {
								"id" : id,
								"deliverType" : value,
								"deliverUid" : deliverUid,
								"deliverShopNo" : deliverShopNo
							}
							httpDeliverSubmit(data);
						});

		var httpDeliverSubmit = function(params) {
			var url = "${pageContext.request.contextPath}/admin/shop/deliver/switch.do";
			$.ajax({
				type : "post",
				dataType : "json",
				data : params,
				url : url,
				async : false,
				success : function(data) {
					if (data.code == 0) {

					} else {
						bootbox.alert(data.msg);
						window.location.reload();
					}
				},
				error : function() {
					bootbox.alert("网络不稳定,运行失败");
				}
			});
		}

		$(".shop_open").change(function() {
			var shopId = $(this).attr("data-id");
			var shop_type = $(this).attr("data-type");
			var state = $(this).val();
			var url = "${pageContext.request.contextPath}/admin/shop/open.do";
			$.ajax({
				type : "post",
				dataType : "json",
				data : {
					"shopId" : shopId,
					"type" : shop_type,
					"state" : state
				},
				url : url,
				async : false,
				success : function(data) {
					if (data.code == 0) {
						bootbox.alert("操作成功");
						window.location.reload();
					} else {
						bootbox.alert("网络不稳定,运行失败");
					}
				},
				error : function() {
					bootbox.alert("网络不稳定,运行失败");
				}
			});
		});
	});
</script>
